﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Week1.world.behavior
{
    class BehaviorRevolve : BehaviorBase
    {
        #region Variables
        protected float[][] fRadius;
        protected float[][] fY;
        private static BehaviorRevolve instance;
        Vector3 oCenter;
        #endregion

        private BehaviorRevolve()
            : base()
        {
            fRadius = new float[MAXREFERENCES][];
            fY = new float[MAXREFERENCES][];
            oCenter = new Vector3();

        }

        /// <summary>
        /// returns the singleton instance of this behavior. This class can be used by maximum of 100 different drawables objects.
        /// </summary>
        public static BehaviorRevolve Instance
        {
            get
            {
                if (instance == null)
                    instance = new BehaviorRevolve();

                return instance;
            }
        }

        /// <summary>
        /// sets the center point
        /// </summary>
        /// <param name="handler"></param>
        /// <param name="instanceIndex"></param>
        /// <param name="name">ignored</param>
        /// <param name="value">global vars</param>
        /*public override void setBehaviorProperty(int handler, int instanceIndex, string name, DrawableGlobalVars value)
        {
            oCenter[handler] = value;
        }
        */

        /// <summary>
        /// Sets the center point
        /// </summary>
        /// <param name="handler"></param>
        /// <param name="instanceIndex"></param>
        /// <param name="name">ignored</param>
        /// <param name="value">vector3 as center</param>
        public override void setBehaviorProperty(int handler, int instanceIndex, string name, ref Vector3 value)
        {
            oCenter = value;
        }

        /// <summary>
        /// Randomly assign radius values to all instances of the model
        /// </summary>
        /// <param name="handlerid"></param>
        /// <param name="oVars"></param>
        public override void initialize(int handlerid, DrawableGlobalVar oVars)
        {
            Random oRnd = new Random();
            float angle = 0;
            float step = 360.0f / oVars.iInstanceCount;
            float radius = 200;
            Vector3 oPosition = new Vector3();

            unsafe
            {
                for (int iDx = 0; iDx < oVars.iInstanceCount; iDx++)
                {
                    oPosition = new Vector3();

                    oVars.fRotateX[iDx] = oRnd.Next(130);
                    oVars.fAngleX[iDx] = angle += step;

                    fRadius[handlerid][iDx] = oRnd.Next((int)radius - 40, (int)radius);
                    fY[handlerid][iDx] = oRnd.Next(-5, 5);


                    oPosition.X = helper.PathHelper.getX(oVars.fAngleX[iDx], fRadius[handlerid][iDx]) + oCenter.X;
                    oPosition.Y = oVars.oPosition[iDx].Y = fY[handlerid][iDx] + oCenter.Y;
                    oPosition.Z = helper.PathHelper.getY(oVars.fAngleX[iDx], fRadius[handlerid][iDx]) + oCenter.Z;

                    oVars.oPosition[iDx] = oPosition;

                    oVars.oWorld[iDx] = Matrix.CreateScale(2) * Matrix.CreateWorld(oVars.oPosition[iDx], Vector3.Forward, Vector3.Up);
                }
            }
        }

        public override void execute(int handlerid, DrawableGlobalVar oVars, GameTime gameTime)
        {

            if (isTriggerActive(handlerid))
            {
                unsafe
                {
                    for (int iDx = 0; iDx < oVars.iInstanceCount; iDx++)
                    {
                        oVars.fAngleX[iDx] += .3f;

                        oVars.oPosition[iDx].X = helper.PathHelper.getX(oVars.fAngleX[iDx], fRadius[handlerid][iDx]) + oCenter.X;
                        oVars.oPosition[iDx].Y = oVars.oPosition[iDx].Y = fY[handlerid][iDx] + oCenter.Y;
                        oVars.oPosition[iDx].Z = helper.PathHelper.getY(oVars.fAngleX[iDx], fRadius[handlerid][iDx]) + oCenter.Z;

                        oVars.oWorld[iDx].M41 = oVars.oPosition[iDx].X;
                        oVars.oWorld[iDx].M42 = oVars.oPosition[iDx].Y;
                        oVars.oWorld[iDx].M43 = oVars.oPosition[iDx].Z;
                    }
                }
            }

        }

        public override int getBehaviorHandlerID(DrawableGlobalVar oVars)
        {
            fRadius[iHandlerIDIndex] = new float[oVars.iInstanceCount];
            fY[iHandlerIDIndex] = new float[oVars.iInstanceCount];
            return base.getBehaviorHandlerID(oVars);
        }
    }
}